#ifndef __PULSE_AUDIO_IO_H__
#define __PULSE_AUDIO_IO_H__ 1

#include "sample_io.h"
#include <pulse/simple.h>
#include <stdint.h>

class CPulseAudioIO : public ISampleIO
{
public:
	// for capture
	CPulseAudioIO(EAction pAction, unsigned int pChannels);

	// for playback
	CPulseAudioIO(EAction pAction,
			unsigned int pChannels,
			NAudioFormats::SStreamInfo pStreamInfo);

	// for capture and playback
	CPulseAudioIO(EAction pAction);

	virtual ~CPulseAudioIO();

	/* START methods inherited by ISampleIO */
	virtual int init(CPropertiesReader *pConfig);
	virtual int destroy();

	virtual int read(void** pData, EAction& pAction, int pTimeout = 1000);
	virtual int write(void* pData, const EAction& pAction, int pLength);

	virtual NAudioFormats::SStreamInfo getStreamInfo();
	/* END methods inherited by ISampleIO */

private:
	enum EInitError {
		INIT_NO_ERROR          = 0,

		// errors
		INIT_FAILURE           = 1,
		INIT_CAPTURE_ERROR     = 2,
		INIT_PLAYBACK_ERROR    = 3,
	};

	EInitError mError;
	EAction mAction;

	unsigned int mPlaybackChannels;
	pa_simple *mPlaybackHandle;

	unsigned int mCaptureChannels;
	pa_simple *mCaptureHandle;
	char *mCaptureBuffer;


	NAudioFormats::SStreamInfo mStreamInfo;
	unsigned int mBytePerSample;

	bool mDoPlayback;
	bool mDoCapture;
	const char *mCaptureSource;
	const char *mPlaybackSink;


	CPulseAudioIO(const CPulseAudioIO& );
	CPulseAudioIO& operator=(const CPulseAudioIO& );

	bool initPlayback();
	bool initCapture();
	unsigned int getBytePerSample();

	/**
	 * Save all sample informations to pSampleSpec.
	 * This includes:
	 * pa_sample_format_t format - The sample format.
	 * uint32_t	rate - The sample rate.
	 * uint8_t channels - Audio channels.
	 *
	 * @param pSampleSpec For storing the sample specifications
	 * @param pForCapture If true then mCaptureChannels will be saved to
	 *        channels. Otherwise mPlaybackChannels
	 * @return True for successful execution
	 **/
	bool getPASampleSpecsFromStreamInfo(pa_sample_spec &pSampleSpec,
			bool pForCapture);


	static const std::string sIniSectionName;
	static const std::string sConfPlaybackSink;
	static const std::string sConfCaptureSource;
};

#endif /* __PULSE_AUDIO_IO_H__ */
